<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class order_mod extends module
{
    function __construct()
    {
        $this->uid = @$_SESSION['user']['uid'];
        $this->spid = @$_SESSION['shop']['spid'];
        parent::__construct();
    }
    public function order_list($arr)
    {
        $page = isset($arr['page']) ? $arr['page'] : '1';
        $page_size = isset($arr['page_size']) ? $arr['page_size'] : '20';
        $user_type = isset($arr['user_type']) ? $arr['user_type'] : '1';
        $page_type = isset($arr['page_type']) ? $arr['page_type'] : 1;
        $status = isset($arr['status']) ? $arr['status'] : '';
        $order_id = isset($arr['order_id']) ? $arr['order_id'] : '';
        $wsql = '';
        if (!empty($status)) {
            $wsql = ' order_status = ' . $status . ' and ';
        }
        if (!empty($order_id)) {
            $wsql = ' order_no = ' . $order_id . ' and ';
        }
        if ($user_type == '1') {
            $wsql .= ' uid = ' . $this->uid . ' and del_user = 0 and ';
        } else {
            $wsql .= ' shop_id = ' . $this->spid . ' and del_shop = 0 and ';
        }
        if ($wsql) {
            $wsql = ' where ' . trim($wsql, ' and ');
        }
        $oinfo = $this->db->get_all("select * from " . table('order_info') . $wsql . " order by id desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($oinfo) {
            foreach ($oinfo as $v) {
                $spid_arr[] = $v['shop_id'];
                $order_id_arr[] = $v['id'];
                if ($v['otype'] == '2' && $v['order_status'] == '8') {
                }
            }
            if (isset($spid_arr)) {
                $spid_in = implode(',', $spid_arr);
                $spall = $this->db->get_all("select id,name from " . table('shop') . " where id in(" . $spid_in . ")");
                if ($spall) {
                    foreach ($spall as $v) {
                        $sp_arr[$v['id']] = $v['name'];
                    }
                }
            }
            if (isset($order_id_arr)) {
                $order_id_in = implode(',', $order_id_arr);
                $order_goods = $this->db->get_all("select * from " . table('order_goods') . " where oid in (" . $order_id_in . ")" . ' order by id desc');
                if ($order_goods) {
                    foreach ($order_goods as $v) {
                        if (!empty($v['sku_value'])) {
                            $v['sku'] = unserialize($v['sku_value']);
                        }
                        $gdata[$v['oid']][] = $v;
                    }
                }
                foreach ($oinfo as $v) {
                    $v['shop_name'] = $sp_arr[$v['shop_id']];
                    $v['order_status_cn'] = $this->order_status($v['order_status']);
                    $v['addtime'] = _date($v['addtime']);
                    $v['span'] = count($gdata[$v['id']]);
                    $v['glist'] = $gdata[$v['id']];
                    $list[] = $v;
                }
                $result['list'] = $list;
                $total = $this->db->get_one("select count(id) as total from " . table('order_info') . $wsql);
                $result['total'] = $total['total'];
                if ($page_size < $total['total']) {
                    $result['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total'], 'page_type' => $page_type));
                }
                return $result;
            }
        }
    }
    function get_detail($arr)
    {
        $order_no = isset($arr['order_no']) ? $arr['order_no'] : exit('order_no');
        $is_shop = isset($arr['is_shop']) ? $arr['is_shop'] : false;
        if ($is_shop) {
            $wsql = " and shop_id = " . $this->spid;
        } else {
            $wsql = " and uid = " . $this->uid;
        }
        $row = $this->db->get_one("select * from " . table('order_info') . " where order_no = " . $order_no . $wsql);
        if ($row['id']) {
            if ($row && $row['uid']) {
                $row['addtime'] = _date($row['addtime']);
                $row['pay_time'] = _date($row['pay_time']);
                $row['deliver_time'] = _date($row['deliver_time']);
                $row['receipt_time'] = _date($row['receipt_time']);
                $row['comment_time'] = _date(@$row['comment_time']);
                $row['order_status_cn'] = $this->module('order')->order_status($row['order_status']);
            }
            $data['order'] = $row;
            $kd_row = $this->db->get_one("select * from " . table('order_address') . " where oid = " . $row['id']);
            if ($row['order_status'] > '2') {
                if ($kd_row['kd_code']) {
                    $express_row = $this->module('express')->get_config($kd_row['kd_code'], 'code');
                    if (isset($express_row['name'])) {
                        $kd_row['kd_name'] = $express_row['name'];
                    }
                }
            }
            $data['express'] = $kd_row;
            $data['glist'] = $this->goods_list($row['id']);
            $data['shop'] = $this->module('shop')->get_one($row['shop_id']);
            return $data;
        } else {
            return false;
        }
    }
    function goods_list($id)
    {
        $query = $this->db->get_all("select * from " . table('order_goods') . " where oid = " . $id);
        if ($query) {
            foreach ($query as $v) {
                if (!empty($v['sku_value'])) {
                    $v['sku'] = unserialize($v['sku_value']);
                }
                $v['total_price'] = _price($v['price']);
                $data[] = $v;
            }
            return $data;
        } else {
            return false;
        }
    }
    public function cencel_order($order_no, $reason)
    {
        if (!empty($reason)) {
            $row = $this->db->get_one("select id,uid,order_no,order_status from " . table('order_info') . " where uid = " . $this->uid . " and order_no = " . $order_no);
            if ($row['id']) {
                if ($row['order_status'] == '1') {
                    $query = $this->cancel_order_run($row['id'], $reason);
                    if ($query) {
                        return array('error' => '0');
                    } else {
                        return array('error' => '1', 'msg' => '取消失败');
                    }
                } else {
                    return array('error' => '1', 'msg' => '当前状态不能取消订单');
                }
            } else {
                return array('error' => '1', 'msg' => '订单不存在');
            }
        } else {
            return array('error' => '1', 'msg' => '取消退款原因不能为空');
        }
    }
    public function cancel_order_run($order_id, $reason)
    {
        $this->db->autocommit();
        $exec = 0;
        $loop = 0;
        $gquery = $this->db->get_all("select id,oid,gid,buy_num,sku_id from " . table('order_goods') . " where oid = " . $order_id);
        if ($gquery) {
            foreach ($gquery as $v) {
                $e_idx = $this->db->query("update " . table('goods_sku_index') . " set stock = stock + " . $v['buy_num'] . " where id = " . $v['sku_id']);
                $e_gds = $this->db->query("update " . table('goods') . " set stock = stock + " . $v['buy_num'] . " where id = " . $v['gid']);
                if ($e_idx == $e_gds) {
                    $exec++;
                }
                $loop++;
            }
        }
        $cancel_oid = $this->db->update(table('order_info'), array('order_status' => '0', 'cancel_reason' => $reason), 'id = ' . $order_id);
        if ($cancel_oid && $exec == $loop) {
            $this->db->commit();
            return true;
        } else {
            $this->db->rollback();
            return false;
        }
    }
    public function group_create_order()
    {
    }
    public function group_cancel()
    {
    }
    public function group_cancel_order($group_id)
    {
        $rall = $this->db->get_all("select * from " . table('promote_group_relate') . " where group_id = " . $group_id);
        if ($rall) {
            $this->db->autocommit();
            foreach ($rall as $v) {
                $order_id = $v['order_id'];
                $pay_row = $this->db->get_one("select id,order_no,total_price from " . table('order_info') . " where id = " . $order_id);
                $q1 = $this->module('account')->add($v['uid'], $pay_row['total_price'], '拼团失败，退款支付。', '6', $group_id);
                $q2 = $this->db->update(table('order_info'), array('order_status' => '0', 'cancel_reason' => '拼团失败'), 'id = ' . $order_id);
                $q3 = $this->db->update(table('order_goods'), array('order_status' => '0'), 'oid = ' . $order_id);
                if ($q1 && $q2 && $q3) {
                    $count[] = '';
                }
            }
            $q5 = $this->db->update(table('promote_group_order'), array('status' => '4'), "id = " . $group_id);
            if (count($rall) == count($count) && $q5) {
                $this->db->commit();
            } else {
                $this->db->rollback();
            }
        }
    }
    public function receive_goods($oid)
    {
        $order_row = $this->db->get_one("select id,order_status,order_no,uid,shop_id,total_price,goods_price,express_price,promote_price from " . table('order_info') . " where uid = " . $this->uid . " and order_no = '" . $oid . "'");
        if ($order_row['id'] && $order_row['order_status'] == '3') {
            $oarr['id'] = $order_row['id'];
            $oarr['uid'] = $order_row['uid'];
            $oarr['shop_id'] = $order_row['shop_id'];
            $oarr['order_no'] = $order_row['order_no'];
            $oarr['total_price'] = $order_row['total_price'];
            $oarr['goods_price'] = $order_row['goods_price'];
            $oarr['express_price'] = $order_row['express_price'];
            $oarr['promote_price'] = $order_row['promote_price'];
            $result = $this->receipt_goods_run($oarr);
            if ($result) {
                return true;
            } else {
                return array('error' => '1', 'msg' => '确认收货失败');
            }
        } else {
            return array('error' => '1', 'msg' => '订单不存在');
        }
    }
    function receipt_goods_run($order_row)
    {
        $time = time();
        $this->db->autocommit();
        $order_query = $this->db->update(table('order_info'), array('order_status' => '4', 'receipt_time' => $time), 'id = ' . $order_row['id']);
        $order_gs_query = $this->db->update(table('order_goods'), array('order_status' => '4'), "oid = " . $order_row['id']);
        $pay_integral = floor($order_row['total_price'] / cfg('user_integral_ratio'));
        $jf_query = $this->db->query("update " . table('member') . " set `integral`=`integral`+" . $pay_integral . " where `uid` = " . $order_row['uid'] . " limit 1;");
        $spRow = $this->db->get_one("select id,uid,ratio from " . table('shop') . " where id = " . $order_row['shop_id'] . ' limit 1');
        $service_price = '0';
        $distribute_price = '0';
        $vquery = $this->db->get_all("select id,oid,gid,price,title,buy_num,commission_rate from " . table('order_goods') . " where oid = " . $order_row['id']);
        foreach ($vquery as $v) {
            $b_query = $this->db->query("update " . table('goods') . " SET `volume`=`volume`+" . $v['buy_num'] . " WHERE id = " . $v['gid'] . " limit 1;");
            if ($b_query) {
                $count[] = '';
            }
            $pay_goods_price = $v['price'] * $v['buy_num'];
            if ($spRow['ratio'] > '0') {
                $service_price = floor($spRow['ratio'] * $pay_goods_price * 100) / 100;
            } else {
                $service_price = '0';
            }
            $service_price += $service_price;
        }
        $sparr['order_id'] = $order_row['id'];
        $sparr['order_no'] = $order_row['order_no'];
        $sparr['shop_id'] = $order_row['shop_id'];
        $sparr['price'] = $order_row['total_price'];
        $income_price = $order_row['total_price'] - $service_price - $distribute_price;
        $sparr['price_income'] = $income_price;
        $sparr['price_service'] = $service_price;
        $sparr['price_distribute'] = $distribute_price;
        $sparr['price_goods'] = $order_row['goods_price'];
        $sparr['price_express'] = $order_row['express_price'];
        $sparr['price_promote'] = $order_row['promote_price'];
        $sparr['receipt_time'] = $time;
        $sp_bill_query = $this->db->add(table('shop_bill'), $sparr);
        if ($order_query && $order_gs_query && $jf_query && count($vquery) == count($count)) {
            $this->db->commit();
            return true;
        } else {
            $this->db->rollback();
            return false;
        }
    }
    public function get_comment($oid)
    {
        $row = $this->db->get_one("select id,uid,shop_id,order_no,order_status,status from " . table('order_info') . " where uid = " . $this->uid . " and order_no = " . $oid);
        if ($row['order_status'] == 4 || $row['order_status'] == 5) {
            $query = $this->db->get_all("select * from " . table('order_goods') . " where oid = " . $row['id']);
            if ($query) {
                foreach ($query as $k => $v) {
                    $gsrow = $this->db->get_one("select * from " . table('goods_comment') . " where ogid = " . $v['id']);
                    if ($gsrow['id']) {
                        $v['comment_status'] = $gsrow['status'];
                        $v['comment_score'] = $gsrow['score'];
                        $v['comment_content'] = $gsrow['content'];
                        $img_data = $this->db->get_all("select * from " . table('goods_comment_img') . " where comment_id = " . $gsrow['id']);
                        $v['comment_img'] = $img_data;
                    }
                    $data[] = $v;
                }
            }
            $result['goods_info'] = $data;
            $srow = $this->db->get_one("select * from " . table('order_score') . " where oid = " . $row['id']);
            if ($srow['id']) {
                $result['shop_score'] = $srow;
            }
            $result['order_row'] = $row;
            return $result;
        } else {
            return false;
        }
    }
    public function order_status($str)
    {
        if ($str == '1') {
            $str = '等待支付';
        } elseif ($str == '2') {
            $str = '等待发货';
        } elseif ($str == '3') {
            $str = '确认收货';
        } elseif ($str == '4') {
            $str = '交易结束';
        } elseif ($str == '5') {
            $str = '相互评价';
        } elseif ($str == '6') {
            $str = '售后/退款';
        } elseif ($str == '8') {
            $str = '拼团分享';
        } elseif ($str == '0') {
            $str = '交易关闭';
        }
        return $str;
    }
    function auto_cancel()
    {
        $cancel_time = cfg('sys_cancel_order_time');
        $overtime = time() - $cancel_time;
        $query = $this->db->get_all("select id,order_status,addtime from " . table('order_info') . " where order_status = 1 and addtime < " . $overtime);
        if ($query) {
            foreach ($query as $v) {
                $this->cancel_order_run($v['id'], '自动取消订单');
            }
        }
    }
    function auto_receipt()
    {
        $overtime = time() - cfg('sys_receipt_time');
        $query = $this->db->get_all("select * from " . table('order_info') . " where order_status = 3 and addtime < " . $overtime);
        if ($query) {
            foreach ($query as $v) {
                $oarr['id'] = $v['id'];
                $oarr['uid'] = $v['uid'];
                $oarr['shop_id'] = $v['shop_id'];
                $oarr['order_no'] = $v['order_no'];
                $oarr['total_price'] = $v['total_price'];
                $oarr['goods_price'] = $v['goods_price'];
                $oarr['express_price'] = $v['express_price'];
                $oarr['promote_price'] = $v['promote_price'];
                $this->receipt_goods_run($oarr);
            }
        }
    }



}